package com.tanhua.admin.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.tanhua.admin.service.AdminService;

import com.tanhua.model.domain.Admin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@RestController
@RequestMapping("/system/users")
public class SystemController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @GetMapping("/verification")
    public ResponseEntity verification(String uuid, HttpServletResponse response) throws IOException {
        //通过Hutool工具生成图片
        CircleCaptcha circleCaptcha = CaptchaUtil.createCircleCaptcha(290, 100);
        //获得图片中的验证码
        String code = circleCaptcha.getCode();
        //将验证码存入redis,有效期是一分钟
        redisTemplate.opsForValue().set(uuid, code, 1, TimeUnit.MINUTES);
        //通过输出流将图片写回前端
        circleCaptcha.write(response.getOutputStream());
        return ResponseEntity.ok(null);
    }


    @PostMapping("/login")
    public ResponseEntity login(@RequestBody Map map) {
        Map map1 = adminService.login(map);
        return ResponseEntity.ok(map1);
    }

    @PostMapping("/profile")
    public ResponseEntity profile() {
        Admin admin = adminService.getprofile();
        return ResponseEntity.ok(admin);
    }
}
